package com.android.project.games;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.android.project.framework.Game;
import com.android.project.framework.Input.TouchEvent;
import com.android.project.framework.gl.Camera2D;
import com.android.project.framework.gl.SpriteBatcher;
import com.android.project.framework.impl.GLScreen;
import com.android.project.framework.math.OverlapTester;
import com.android.project.framework.math.Rectangle;
import com.android.project.framework.math.Vector2;




public class HighScoreScreen extends GLScreen{
	Camera2D guiCam;
	SpriteBatcher batcher;
	Rectangle backBounds;
	Vector2 touchPoint;
	String[] highScores;
	float xOffset = 0;

	public HighScoreScreen(Game game) {
		super(game);
		// TODO Auto-generated constructor stub
		guiCam = new Camera2D(glGraphics, 320, 480);
		batcher = new SpriteBatcher(glGraphics, 100);
		touchPoint = new Vector2();
		backBounds = new Rectangle(0, 0, 64 ,64);
		highScores = new String[5];
		for (int i = 0 ; i < 5 ; i++){
			//highScores[i] = (i +1) + ". " + Settings.highscores[i];
			highScores[i] = "" + Settings.highscores[i];
			//xOffset = Math.max(highScores[i].length() * Assets.font.glyphWidth,xOffset);
		}
		//xOffset = 160 - xOffset / 2; //serve per centrare la stringa
	}

	@Override
	public void update(float deltaTime) {
		// TODO Auto-generated method stub
		List<TouchEvent> touchEvent = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();
		int len = touchEvent.size();
		for (int i = 0; i < len; i++){
			TouchEvent event = touchEvent.get(i);
			if(event.type == TouchEvent.TOUCH_UP){
				touchPoint.set(event.x, event.y);
				guiCam.touchToWorld(touchPoint);
				if(OverlapTester.pointInRectangle(backBounds, touchPoint)){
					Assets.playSound(Assets.clickSound);
					game.setScreen(new MainMenuScreen(game));
					return;	
				}
			}
			
		}
	}

	@Override
	public void present(float deltaTime) {
		// TODO Auto-generated method stub
		GL10 gl = glGraphics.getGl();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		guiCam.setViewportAndMatrices();
		gl.glEnable(GL10.GL_TEXTURE_2D);
		batcher.beginBatch(Assets.background);
		batcher.drawSprite(160, 240, 320, 480, Assets.backgroundRegion);
		batcher.endBatch();
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		batcher.beginBatch(Assets.items);
		batcher.drawSprite(160, 420, 293, 64, Assets.hightText);
		batcher.drawSprite(160, 240, 300, 220, Assets.high);
		Assets.font.drawText(batcher, "RANK", 58, 320);
		Assets.font.drawText(batcher, "SCORE", 230, 320);
		Assets.font.drawText(batcher, "----------------------", 58, 300);
		float y = 200;
		for(int i = 4; i >= 0; i--) {
			Assets.font.drawText(batcher, "" + ( i + 1), 62, y);
			Assets.font.drawText(batcher, highScores[i], 232, y);
			y += Assets.font.glyphHeight;
		}
		batcher.drawSprite(32, 32, 64 ,64, Assets.backRegion);
		batcher.endBatch();
		gl.glDisable(GL10.GL_BLEND);
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

}
